package com.ruoyi.combat.repository;

import com.ruoyi.combat.domain.neo4j.ForecastOrgNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ForecastOrgNodeRepository extends Neo4jRepository<ForecastOrgNode, Long> {

    @Query(value = "MATCH (n:ForecastOrg) WHERE n.name = $orgName " +
            "AND n.address = $ip " +
            "RETURN n")
    Optional<ForecastOrgNode> findByNameAndIP(@Param("orgName") String orgName, @Param("ip") String ip);

    // 分页查询
    @Query(value = "MATCH (n:ForecastOrg) " +
            "RETURN n SKIP $pageNum * $pageSize LIMIT $pageSize",
            countQuery = "MATCH (n:ForecastOrg) " +
                    "RETURN count(n)")
    Page<ForecastOrgNode> findByNamePage(
            @Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,  Pageable pageable
    );

    // 根据组织名称和ip删除这条数据
    @Query("MATCH (n:ForecastOrg) WHERE n.name = $orgName AND n.address = $ip DELETE n")
    void deleteByNameAndIP(@Param("orgName") String orgName, @Param("ip") String ip);
}
